package com.bosch.in.rtp1.ept.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CopyToNetworkFolder {

	/** The Constant LOG. */
	private static final Log LOG = LogFactory.getLog(CopyToNetworkFolder.class);
	
	private static final String FILECOPY_SRC_ERROR = "File being copied is not existed";
	private static final String FILECOPY_SRCFOLDER_ERROR = "Can't copy directory";
	private static final String FILECOPY_SRCREAD_ERROR = "File being copied is not readable";
	private static final String FILECOPY_DEST_ERROR = "Can't write to destination file";
	private static final String FILECOPY_DESTFOLDER_ERROR = "Destination folder is not existed";
	private static final String FILECOPY_DESTREAD_ERROR = "Parent folder is a file";
	private static final String FILECOPY_DESTPERMISSION_ERROR = "Can't write to parent folder";
	private static final String FILECOPY_FAIL = "Network error";
	
	@SuppressWarnings("finally")
	public static String copyFile(File varFromFile, File varToFile)
			throws IOException {
		String result = "";
		if (!varFromFile.exists())
		{
			result = FILECOPY_SRC_ERROR;
			//System.err.println("no such source file: " + varFromFile);
			LOG.error("[COPY TO NETWORK]: no such source file " + varFromFile);
		} else if (!varFromFile.isFile())
		{
			result = FILECOPY_SRCFOLDER_ERROR;
			//System.err.println("can't copy directory: " + varFromFile);
			LOG.error("[COPY TO NETWORK]: Can't copy directoty " + varFromFile);
		} else if (!varFromFile.canRead())
		{
			result = FILECOPY_SRCREAD_ERROR;
			//System.err.println("source file is unreadable: " + varFromFile);
			LOG.error("[COPY TO NETWORK]: source file is unreadable " + varFromFile);
		} else if (varToFile.isDirectory())
		{
			varToFile = new File(varToFile, varFromFile.getName());
			LOG.error("[COPY TO NETWORK]: Destination file is not File " + varToFile);
		} else if (varToFile.exists()) {
			if (!varToFile.canWrite())
			{
				result = FILECOPY_DEST_ERROR;
				LOG.error("[COPY TO NETWORK]: Can't write Destination File: " + varToFile);
			}
		} else {

			String varParent = varToFile.getParent();

			if (varParent == null)
				varParent = System.getProperty("user.dir");

			File vardir = new File(varParent);
			if (!vardir.exists())
			{
				result = FILECOPY_DESTFOLDER_ERROR;
				LOG.error("[COPY TO NETWORK]: Parent Folder is not exist " + vardir);
			} else if (vardir.isFile())
			{
				result = FILECOPY_DESTREAD_ERROR;
				LOG.error("[COPY TO NETWORK]: Parent Folder is a File " + vardir);
				
			} else if (!vardir.canWrite())
			{
				result = FILECOPY_DESTPERMISSION_ERROR;
				LOG.error("[COPY TO NETWORK]: Can't write to Parent Folder " + vardir);
			}
		}

		FileInputStream varFromSource = null;

		FileOutputStream VarToDestination = null;
		try {

			varFromSource = new FileInputStream(varFromFile);

			VarToDestination = new FileOutputStream(varToFile);

			byte[] buffer = new byte[4096];

			int bytes_read;

			while ((bytes_read = varFromSource.read(buffer)) != -1)
				VarToDestination.write(buffer, 0, bytes_read);
			//result = UamConstants.FILECOPY_SUCCESS;
		} catch (Exception e) {
			result = FILECOPY_FAIL;
			LOG.error("[COPY TO NETWORK] : "+e.getMessage(),e);
		} finally {
			if (varFromSource != null) {
				try {
					varFromSource.close();
				} catch (IOException e) {
					result = FILECOPY_FAIL;
					LOG.error("[COPY TO NETWORK] Closing source File : "+e.getMessage(),e);
				}
			}
			if (VarToDestination != null) {
				try {
					VarToDestination.close();
				} catch (IOException e) {
					result = FILECOPY_FAIL;
					LOG.error("[COPY TO NETWORK] Closing dest File : "+e.getMessage(),e);
				}
			}
			return result;
		}
	}

}
